package br.ufrj.ppgi.greco.controller;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.ufrj.ppgi.greco.model.dao.DAOService;
import br.ufrj.ppgi.greco.model.entity.EntityService;


@Resource
public class ServiceController
{
	private DAOService ds;
	private Result result;
	
	public ServiceController(DAOService ds, Result result)
	{
		this.ds = ds;
		this.result = result;
	}
	
	
	@Get
	@Path("/config/services")
	public void listAllServices()
	{
		this.result.include("services",this.ds.getAll());
	}
	
	@Get
	@Path("/config/services/new")
	public void newService()
	{	
	}
	
	@Post
	@Path("/config/services/create")
	public void createService(EntityService es)
	{
		this.ds.insert(es);
		this.result.redirectTo(ServiceController.class).listAllServices();
	}
	
	@Get
	@Path("/config/services/{id}")
	public void listService(int id)
	{
		this.result.include("ser",this.ds.getByID(id));
	}
	
	@Post
	@Path("/config/services/update")
	public void updateService(EntityService es)
	{
		this.ds.update(es);
		this.result.redirectTo(ServiceController.class).listAllServices();
	}
	
	
	@Get
	@Path("/config/services/{id}/delete")
	public void deleteService(int id)
	{
		this.ds.deleteByID(id);
		this.result.redirectTo(ServiceController.class).listAllServices();
	}
	
}
